//
//  JgAdsSuspensionViewController.m
//  JgAds-Demo
//
//  Created by 刘娟 on 2025/6/11.
//

#import "JgAdsSuspensionViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "UIView+Toast.h"
#import <JgAdsSDK/JgAdsSuspensionAdView.h>
#import "JgAdsSuspensionTestingPushViewController.h"
@interface JgAdsSuspensionViewController () <UITableViewDelegate, UITableViewDataSource, JgAdsSuspensionAdViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *items;

@property (nonatomic, strong) JgAdsSuspensionAdView *suspensionAdView;

@end

@implementation JgAdsSuspensionViewController

#pragma mark - Life crycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    
    _items = [NSMutableArray new];
    
    _tableView = [UITableView new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    _tableView.frame = self.view.bounds;
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf cleanAllAd];
        [weakSelf loadLocalData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadLocalData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    NSString *posId = @"70ba8e9c61f5";
        
    self.suspensionAdView = [[JgAdsSuspensionAdView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 100 - 10, self.view.frame.size.height/2 + 50, 0, 0) posId:posId];
    self.suspensionAdView.viewController = self;
    self.suspensionAdView.delegate = self;
    [self.suspensionAdView loadRequest];
    [self.view addSubview:self.suspensionAdView];
    self.view.userInteractionEnabled = YES;
}

- (void)dealloc {
    NSLog(@"%s",__func__);
    [self cleanAllAd];
}
#pragma mark - Private method

- (void)cleanAllAd {
    _items = [NSMutableArray new];
    [self.tableView reloadData];
}

- (void)loadLocalData{
    for (int i = 0; i < 15; i ++) {
        [self.items addObject:[NSNull null]];
    }
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [UIView animateWithDuration:1 animations:^{
        self.suspensionAdView.frame = CGRectMake(self.view.frame.size.width - 40, self.suspensionAdView.frame.origin.y, self.suspensionAdView.frame.size.width, self.suspensionAdView.frame.size.height);
    }];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (!decelerate) {
        [UIView animateWithDuration:1 animations:^{
            self.suspensionAdView.frame = CGRectMake(self.view.frame.size.width - self.suspensionAdView.frame.size.width - 10, self.suspensionAdView.frame.origin.y, self.suspensionAdView.frame.size.width, self.suspensionAdView.frame.size.height);
        }];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [UIView animateWithDuration:1 animations:^{
        self.suspensionAdView.frame = CGRectMake(self.view.frame.size.width - self.suspensionAdView.frame.size.width - 10, self.suspensionAdView.frame.origin.y, self.suspensionAdView.frame.size.width, self.suspensionAdView.frame.size.height);
    }];
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    id item = [_items objectAtIndex:indexPath.row];
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.text = [NSString stringWithFormat:@"ListViewitem %li", indexPath.row];
        return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.navigationController pushViewController:[JgAdsSuspensionTestingPushViewController new] animated:YES];
}
#pragma mark - JgAdsSuspensionAdViewDelegate

/**
 *  请求成功
 */
- (void)jgadsSuspensionSuccessLoad:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，非标广告加载成功",__func__);
}

/**
 *  请求失败
 */
- (void)jgadsSuspensionViewFailedToLoadWithError:(NSError *)error{
    NSLog(@"%s，非标广告加载失败",__func__);
}
/**
 *  展示回调
 */
- (void)jgadsSuspensionViewDidPresent:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，非标广告展示成功",__func__);
}
/**
 *  曝光回调
 */
- (void)jgadsSuspensionViewWillExpose:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，非标广告曝光",__func__);
}

/**
 *  点击回调
 */
- (void)jgadsSuspensionViewClicked:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，非标广告点击",__func__);
}

/**
 *  被用户关闭时调用
 */
- (void)jgadsSuspensionViewWillClose:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，非标广告关闭",__func__);
    [_suspensionAdView removeFromSuperview];
    _suspensionAdView = nil;
}

/**
 *  被用户关闭广告落地页调用
 */
- (void)jgadsSuspensionViewCloseLandingPage:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，非标广告关闭落地页",__func__);
}


@end
